package core;

import org.testng.Assert;

import core.helpers.PropertyLoaderHelper;

/**
 * Class provides parameters (constants) for the project
 */
public class Config
{
	protected final Pilot pilot;
	private final PropertyLoaderHelper projectProperties;

	// Servers
	public final static String SERVER_LOCAL = "local";;
	public final static String SERVER_SAUCELABS = "saucelabs";
	// WebDriver
	public static int WEBDRIVER_WAIT;
	public Boolean WEBDRIVER_FLASH_ELEMENTS;
	// Platforms
	public final String PLATFORM_WEB = "web";
	public final String PLATFORM_MOBILE = "mob";
	// Browsers
	public final String BROWSER_FIREFOX = "firefox";
	public final String BROWSER_CHROME = "chrome";
	public final String BROWSER_IEXPLORER = "iexplorer";
	public final String BROWSER_SAFARI = "safari";
	public final String BROWSER_ANDROID = "android";
	public final String BROWSER_IPHONE = "iphone";
	// Browser versions
	public final String BROWSER_FF_DEFAULT_VERSION = "27";
	public final String BROWSER_CH_DEFAULT_VERSION = "32";
	public final String BROWSER_IE_DEFAULT_VERSION = "10";
	public final String BROWSER_ANDROID_VERSION = "4.3";
	// OS
	public final String OS_WIN_7 = "win7";
	public final String OS_WIN_8 = "win8";
	public final String OS_ANDROID = "android";
	// Screen Resolutions
	public final String SCREEN_RESOLUTION_1920x1200 = "1920x1200";
	public final String SCREEN_RESOLUTION_1440x900 = "1440x900";
	public final String SCREEN_RESOLUTION_1280x1024 = "1280x1024";
	public final String SCREEN_RESOLUTION_1024x768 = "1024x768";
	// Google Chrome driver
	public final String DRIVER_CHROME_PATH;
	// Internet Explorer driver
	public final String DRIVER_IE_PATH;
	// Screen recorder
	public boolean SCREENLOGGER_ENABLED = false;
	public String SCREENLOGGER_PATH = "";
	// HAR
	public String HAR_STORAGE_URL;
	public String HAR_REPORTS_PATH;
	// Proxy
	public static Boolean PROXY_AUTH;
	public String PROXY_USERNAME;
	public String PROXY_PASSWORD;
	// Data Base
	public String DB_HOST;
	public String DB_PORT;
	public String DB_NAME;
	public String DB_USER;
	public String DB_PASSWORD;
	// HTTP authentification
	public String HTTP_USERNAME;
	public String HTTP_PASSWORD;
	// Appium
	public String APPIUM_DEVICE;
	public String APPIUM_BROWSER;
	public String APPIUM_PATH;
	public String APPIUM_CHROMEDRIVER;
	public String APPIUM_LOG;
	public String APPIUM_LOGLEVEL;
	public String APPIUM_COMMANDTIMEOUT;
	// Redmine access
	public String REDMINE_USERNAME;
	public String REDMINE_PASSWORD;

	// Sites identifiers
	public final static String SITE_ID_REDMINE_PH = "redmine.platformphoenix.com";
	public final static String SITE_ID_STAFF = "staff.maximagroup.com";

	/**
	 * Constructor
	 * @param p_pilot
	 */
	public Config(Pilot p_pilot)
	{
		pilot = p_pilot;

		projectProperties = loadFileFromRes("project.properties");

		DRIVER_CHROME_PATH = getProjectProperty("webdriver.chrome.path");
		DRIVER_IE_PATH = getProjectProperty("webdriver.ie.path");

		System.setProperty("webdriver.ie.driver", DRIVER_IE_PATH);
		System.setProperty("webdriver.chrome.driver", DRIVER_CHROME_PATH);

		// Proxy
		PROXY_AUTH = Boolean.parseBoolean(getProjectProperty("proxy.auth"));
		PROXY_USERNAME = getProjectProperty("proxy.username");
		PROXY_PASSWORD = getProjectProperty("proxy.password");

		// Data Base
		DB_HOST = getProjectProperty("db.host");
		DB_PORT = getProjectProperty("db.port");
		DB_NAME = getProjectProperty("db.name");
		DB_USER = getProjectProperty("db.user");
		DB_PASSWORD = getProjectProperty("db.password");

		SCREENLOGGER_ENABLED = Boolean.parseBoolean(getProjectProperty("screenlogger.enabled"));
		SCREENLOGGER_PATH = getProjectProperty("screenlogger.path");

		WEBDRIVER_WAIT = Integer.parseInt(getProjectProperty("webdriver.wait"));
		WEBDRIVER_FLASH_ELEMENTS = Boolean.parseBoolean(getProjectProperty("webdriver.flash.element"));

		REDMINE_USERNAME = getProjectProperty("redmine.username");
		REDMINE_PASSWORD = getProjectProperty("redmine.password");

		/** APPIUM SETTINGS **/
		APPIUM_DEVICE = getProjectProperty("appium.device");
		if (!((APPIUM_DEVICE != "device") && (APPIUM_DEVICE != "emulator") && (APPIUM_DEVICE != "any")))
		{
			pilot.log().warn(
					"Incorrect Value specified for 'Appium.Device' Property! Got - '" + APPIUM_DEVICE
							+ "', expected 'device' / 'emulator' or 'any'. Defaulting to 'ANY' behaviour");
			APPIUM_DEVICE = "any";
		}
		APPIUM_BROWSER = getProjectProperty("appium.browser");
		if (APPIUM_BROWSER.equals("default"))
		{
			APPIUM_BROWSER = "Browser";
		}

		APPIUM_PATH = getProjectProperty("appium.path");
		APPIUM_CHROMEDRIVER = getProjectProperty("appium.chromedriver");
		APPIUM_LOG = getProjectProperty("appium.log");
		APPIUM_LOGLEVEL = getProjectProperty("appium.log.level");
		APPIUM_COMMANDTIMEOUT = getProjectProperty("appium.command.timeout");

	}

	/**
	 * Loads a configuration file by relative path (from resources)
	 * @param p_pathToFile
	 * @return PropertyLoaderHelper instance
	 */
	protected PropertyLoaderHelper loadFileFromRes(String p_pathToFile)
	{
		Assert.assertTrue(pilot.file().isFileResExist(p_pathToFile), "Configuration file not found: " + p_pathToFile);

		return new PropertyLoaderHelper(pilot, p_pathToFile);
	}

	/**
	 * Loads a configuration file by full path
	 * @param p_pathToFile
	 * @return PropertyLoaderHelper instance
	 */
	protected PropertyLoaderHelper loadFile(String p_pathToFile)
	{
		Assert.assertTrue(pilot.file().isFileExist(p_pathToFile), "Configuration file not found: " + p_pathToFile);

		return new PropertyLoaderHelper(pilot, p_pathToFile);
	}

	/**
	 * Gets a project property
	 * @param p_propertyName
	 *            Property name in the configuration file
	 * @return Parameter from properties file
	 */
	protected String getProjectProperty(String p_propertyName)
	{
		try
		{
			String propertyValue = projectProperties.loadAsRes(p_propertyName);

			if (propertyValue == null)
			{
				throw new NullPointerException();
			}

			return propertyValue;
		}
		catch (NullPointerException p_ex)
		{
			pilot.log().error("Project property not found: '" + p_propertyName + "'");

			return null;
		}
	}

	/**
	 * Gets user email by name
	 * @param p_userName
	 *            User name
	 * @return User ID
	 */
	public String getUserEmail(String p_userName)
	{
		switch (p_userName)
		{
			case "Babar":
			{
				return "igor.babar@together.com";
			}
			case "Vasilevskaya":
			{
				return "julia.vasilevskaya@together.com";
			}
			case "Lukashova":
			{
				return "elena.lukashova@together.com";
			}
			case "Chudlya":
			{
				return "2404";
			}
			case "Lyapkin":
			{
				return "vadim.lyapkin@together.com";
			}
			case "Kovalenko":
			{
				return "maxim.kovalenko@together.com";
			}
			case "Zemlyanskiy":
			{
				return "alexander.zemlyanskiy@together.com";
			}
			case "Romanichenko":
			{
				return "anton.romanichenko@together.com";
			}
			case "Zaporozhskiy":
			{
				return "artem.zaporozhskiy@together.com";
			}
			case "Zaporozhskaya":
			{
				return "julia.zaporozhskaya@together.com";
			}
			case "Khurtak":
			{
				return "alena.khurtak@together.com";
			}
			case "Penner":
			{
				return "olga.penner@together.com";
			}
			case "Gorkun":
			{
				return "roman.gorkun@together.com";
			}
			case "Golovko":
			{
				return "alexander.golovko@together.com";
			}
			case "Koloskov":
			{
				return "andrey.koloskov@together.com";
			}
			case "Savin":
			{
				return "vitaliy.savin@together.com";
			}
			case "Nagibin":
			{
				return "andrey.nagibin@together.com";
			}
			case "Gatsenko":
			{
				return "denys.gacenko@together.com";
			}
			case "Kachan":
			{
				return "eugeniy.kachan@together.com";
			}
			default:
			{
				return "0";
			}

		}
	}

	/**
	 * Gets user ID in Redmine by name
	 * @param p_userName
	 *            User name
	 * @return User ID
	 */
	// public String getRedmineUserID(String p_userName)
	// {
	// switch (p_userName)
	// {
	// case "Babar":
	// {
	// return "2078";
	// }
	// case "Vasilevskaya":
	// {
	// return "2329";
	// }
	// case "Lukashova":
	// {
	// return "2068";
	// }
	// case "Chudlya":
	// {
	// return "2225";
	// }
	// case "Lyapkin":
	// {
	// return "2192";
	// }
	// case "Kovalenko":
	// {
	// return "2228";
	// }
	// case "Zemlyanskiy":
	// {
	// return "2184";
	// }
	// case "Romanichenko":
	// {
	// return "2188";
	// }
	// case "Zaporozhskiy":
	// {
	// return "2242";
	// }
	// case "Zaporozhskaya":
	// {
	// return "2243";
	// }
	// case "Larionova":
	// {
	// return "2159";
	// }
	// case "Penner":
	// {
	// return "2185";
	// }
	// case "Gorkun":
	// {
	// return "2227";
	// }
	// case "Golovko":
	// {
	// return "100";
	// }
	// case "Koloskov":
	// {
	// return "2230";
	// }
	// case "Savin":
	// {
	// return "2183";
	// }
	// case "Nagibin":
	// {
	// return "2191";
	// }
	// case "Gatsenko":
	// {
	// return "1986";
	// }
	// case "Pinchuk":
	// {
	// return "2500";
	// }
	// case "Cherniy":
	// {
	// return "2490";
	// }
	// case "Vavrik":
	// {
	// return "2523";
	// }
	// case "Timoshenko":
	// {
	// return "2508";
	// }
	// case "Gubar":
	// {
	// return "2562";
	// }
	// case "Tashlikov":
	// {
	// return "2550";
	// }
	// case "Kachan":
	// {
	// return "2647";
	// }
	// case "Kulinich":
	// {
	// return "2685";
	// }
	// case "Vasileyko":
	// {
	// return "2684";
	// }
	// case "Shatalov":
	// {
	// return "2683";
	// }
	// case "Bessmertniy":
	// {
	// return "2756";
	// }
	// default:
	// {
	// return "0";
	// }
	// }
	// }

}
